package top.melody.ecommerce.webapps.www.base;

import top.melody.ecommerce.common.util.HTTPUtil;
import top.melody.ecommerce.common.util.JsonMapper;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author: blentle
 * @group: rd
 * @createdate: 2016/11/11 9:55
 * @mail: blentle.huan.ren@gmail.com
 * @description: web 上下文辅助类...
 * @since:1.0
 */
public class AssertContext {

    public static final String ENCODE_UTF8 = "UTF-8";

    public static final String INDEX_TAG = "/";

    public static UserSession getCurrentAccount(HttpServletRequest request) throws UnsupportedEncodingException {
        UserSession userSession = getCurrentAccountFromSession(request);
        if(userSession == null)
            userSession = getCurrentAccountFromCookie(request);
        return userSession;
    }

    public static UserSession getCurrentAccountFromSession(HttpServletRequest request) {
        UserSession userSession = (UserSession)request.getSession(true).getAttribute(Constant.SESSION_ACCOUNT_KEY);
        return userSession;
    }

    public static void putCurrentAccount2Session(UserSession currentUser, HttpServletRequest request) {
        request.getSession().setAttribute(Constant.SESSION_ACCOUNT_KEY, currentUser);
    }

    public static void putSmsCode2Session(String smsCode, HttpServletRequest request) {
        request.getSession().setAttribute(Constant.SESSION_SIGNUP_SMS_KEY, smsCode);
    }

    public static String getSmsCodeFromSession(HttpServletRequest request) {
        String smsCode = (String)request.getSession().getAttribute(Constant.SESSION_SIGNUP_SMS_KEY);
        return smsCode;
    }

    public static void removeFromSession(String sessionKey, HttpServletRequest request) {
        request.getSession().removeAttribute(sessionKey);
    }

    public static void putCurrentAccount2Cookie(UserSession userSession, int expiredSeconds, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String name = URLEncoder.encode(Constant.COOKIE_ACCOUNT_KEY, ENCODE_UTF8);
        String value = URLEncoder.encode(JsonMapper.nonEmptyMapper().toJson(userSession), ENCODE_UTF8);
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(expiredSeconds);
        cookie.setPath(getContextPath(request) + INDEX_TAG);
        HTTPUtil.setCookie(response,cookie);
    }

    public static UserSession getCurrentAccountFromCookie(HttpServletRequest request) throws UnsupportedEncodingException {
        String cookieValue = HTTPUtil.getCookieValue(request, Constant.COOKIE_ACCOUNT_KEY);
        return JsonMapper.nonEmptyMapper().fromJson(cookieValue, UserSession.class);
    }

    public static void removeFromCookie(String key, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        key = URLEncoder.encode(key, ENCODE_UTF8);
        if (request.getCookies() != null) {
            for (Cookie c : request.getCookies()) {
                if (c.getName().equals(key)) {
                    c.setPath(request.getContextPath() + INDEX_TAG);
                    HTTPUtil.removeCookie(request, response, c);
                    break;
                }
            }
        }
    }

    public static String getContextPath(HttpServletRequest request) {
        return request.getContextPath();
    }
}
